﻿using AdminStystem.Domain;
using AdminSytem.Application;
using AdminSytem.Application.Dto;
using AdminSytem.Application.Login;
using AdminSytem.Application.Traceabilities;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace AdminSytem.WebApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class AdminStystemController : ControllerBase
    {
        private readonly IAdminService adminService;
        private readonly ITraceabilityServices traceabilityServices;

        public AdminStystemController(IAdminService adminService, ITraceabilityServices traceabilityServices)
        {
            this.adminService = adminService;
            this.traceabilityServices = traceabilityServices;
        }
        [HttpGet]
        public async Task<IActionResult> LoginQuery(string? UserName, string? UserPass)
        {
            return Ok(adminService.LoginQuery(UserName, UserPass));
        }
        [HttpPost]
        public Task<ResultDto<int>> InsertTraceability(InsertTraceabilityDto dto)
        {
            return traceabilityServices.InsertTraceability(dto);
        }
        [HttpGet]
        public Task<ResultDto<ApiPaiing<TraceabilityDto>>> GetTraceabilitylist(int index, int size)
        {
            return traceabilityServices.GetTraceability(index, size);
        }
        [HttpGet]
       public async Task<ResultDto<List<TraceabilityDto>>> Gettraceability(int id)
        {
            return await traceabilityServices.Gettraceability(id);
        }
        [HttpGet]
        public Task<ResultDto<List<Goods>>> GetGoods()
        {
            return traceabilityServices.GetGoods();
        }
        [HttpPost]
        public IActionResult uploadFile(IFormFile file)
        {
            var filename = file.FileName;
            var path = Directory.GetCurrentDirectory() + @"\wwwroot\Image\" + filename;
            using (FileStream fs = new FileStream(path, FileMode.Create))
            {
                file.CopyTo(fs);

            }
            var url = "/Image/" + filename;
            return Ok(url);
        }
        [HttpGet] 
        public  Task<ResultDto<List<Traceability>>> GetOnly(int id)
        {
            return traceabilityServices.GetOnly(id);
        }
        [HttpPost]
        public Task<ResultDto<Traceability>> Deltraceability(int id)
        {
            return traceabilityServices.Deltraceability(id);
        }
        [HttpPost]
       public Task<ResultDto<int>> UpdateTraceability(UpdateTraceabilityDto dto)
        {
            return traceabilityServices.UpdateTraceability(dto);
        }
    }
}
